﻿Public Class KhachHangDAO

    Public Shared Function KhachHang_Load() As DataTable
        Return DataProvider.LoadTable("Select * from KhachHang")
    End Function

    Public Shared Sub KhachHang_Insert(ByVal kh As KhachHangDTO)
        Dim Npara As Integer = 7
        Dim sql As String = "usp_KHACHHANG_InsertKHACHHANG"
        Dim name As String() = New String(Npara - 1) {}
        Dim value As Object() = New Object(Npara - 1) {}
        name(0) = "@MAKH"
        name(1) = "@MANHA"
        name(2) = "@HOTEN"
        name(3) = "@CMND"
        name(4) = "@SDT"
        name(5) = "@NgayThueNha"
        name(6) = "@NgayketThuc"

        kh.Makh = AutoInc.AutoIncStr(KhachHangDAO.KhachHang_Load, "MAKH", "KH")
        value(0) = kh.Makh
        value(1) = kh.Manha
        value(2) = kh.Hoten
        value(3) = kh.Cmnd
        value(4) = kh.Sdt
        value(5) = kh.NgayThueNha
        value(6) = kh.NgayKetThuc

        DataProvider.ExecuteNonQueryPara(sql, name, value, Npara)
        'Sau khi khach hang thue nha Dong thoi chuyen tu Tinh Trang nha 0 -> 1
        sql = String.Format("update NHA set TINHTRANG = 1 where MANHA = '{0}'", kh.Manha)
        DataProvider.ExecuteNonQuery(sql)
        'Cap nhat tinh trang nha thanh cong
    End Sub

    Public Shared Sub KhachHang_Update(ByVal kh As KhachHangDTO)
        Dim Npara As Integer = 7
        Dim sql As String = "usp_KHACHHANG_UpdateKHACHHANG"
        Dim name As String() = New String(Npara - 1) {}
        Dim value As Object() = New Object(Npara - 1) {}
        name(0) = "@MAKH"
        name(1) = "@MANHA"
        name(2) = "@HOTEN"
        name(3) = "@CMND"
        name(4) = "@SDT"
        name(5) = "@NgayThueNha"
        name(6) = "@NgayketThuc"

        value(6) = kh.NgayKetThuc
        value(5) = kh.NgayThueNha
        value(0) = kh.Makh
        value(1) = kh.Manha
        value(2) = kh.Hoten
        value(3) = kh.Cmnd
        value(4) = kh.Sdt
        DataProvider.ExecuteNonQueryPara(sql, name, value, Npara)
    End Sub
    Public Shared Sub KhachHang_Delete(ByVal kh As KhachHangDTO)
        Dim sql As String = String.Format("delete KhachHang where MAKH = '{0}'", kh.Makh)
        DataProvider.ExecuteNonQuery(sql)
        'Sau khi delete khach hang chuyen nha tu Tinh Trang nha 1 -> 0
        sql = String.Format("update NHA set TINHTRANG = 0 where MANHA = '{0}'", kh.Manha)
        DataProvider.ExecuteNonQuery(sql)
        'Cap nhat tinh trang nha thanh cong
    End Sub
End Class
